package cn.mw.cmdb.virtualization.service.handler;

import cn.mw.cmdb.virtualization.dto.VirtualizationBasicInfoDto;
import cn.mw.cmdb.virtualization.dto.VirtualizationTreeDto;
import cn.mw.microMonitorCommon.enums.UnicloudTypeEnum;
import cn.mw.microMonitorCommon.enums.VcenterTypeEnum;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description 虚拟化树结构处理
 */
public class VirtualizationTreeHandler {

    public void handler(List<VirtualizationTreeDto> roots){
        for (VirtualizationTreeDto root : roots) {
            vmTypeClassify(root);
            List<VirtualizationTreeDto> virtualizationTreeDtos = flattenTree(root);
            Map<String, List<VirtualizationTreeDto>> dtoMap = virtualizationTreeDtos.stream().filter(item->item.getVmType() != null).collect(Collectors.groupingBy(item -> item.getVmType()));
            handlerData(root,dtoMap);
        }
    }


    public void vmTypeClassify(VirtualizationTreeDto root){
        List<VirtualizationTreeDto> children = root.getChildren();
        if(CollectionUtils.isNotEmpty(children)){
            for (VirtualizationTreeDto child : children) {
                List<VirtualizationTreeDto> virtualizationTreeDtos = flattenTree(child);
                Map<String, List<VirtualizationTreeDto>> dtoMap = virtualizationTreeDtos.stream().collect(Collectors.groupingBy(item -> item.getVmType()));
                handlerData(child,dtoMap);
                vmTypeClassify(child);
            }
        }
    }


    public List<VirtualizationTreeDto> flattenTree(VirtualizationTreeDto root) {
        List<VirtualizationTreeDto> flatList = new ArrayList<>();
        flattenTreeRecursively(root, flatList);
        return flatList;
    }

    private void flattenTreeRecursively(VirtualizationTreeDto node, List<VirtualizationTreeDto> flatList) {
        if (node == null) {
            return;
        }
        flatList.add(node);
        if (node.getChildren() != null) {
            for (VirtualizationTreeDto child : node.getChildren()) {
                flattenTreeRecursively(child, flatList);
            }
        }
    }

    public void handlerData(VirtualizationTreeDto root,Map<String, List<VirtualizationTreeDto>> vmTypeMap){
        List<VirtualizationBasicInfoDto> basicInfoDtos = new ArrayList<>();
        VcenterTypeEnum[] vcenterTypeEnums = VcenterTypeEnum.values();
        boolean flag = false;
        for (VcenterTypeEnum vcenterTypeEnum : vcenterTypeEnums) {
            VirtualizationBasicInfoDto basicInfoDto = new VirtualizationBasicInfoDto();
            if(vmTypeMap.containsKey(vcenterTypeEnum.getType())){
                basicInfoDto.setName(vcenterTypeEnum.getDesc());
                basicInfoDto.setValue(vmTypeMap.get(vcenterTypeEnum.getType()).size());
                basicInfoDtos.add(basicInfoDto);
                flag = true;
            }
        }
        if(flag){
            root.setBasicInfoData(basicInfoDtos);
            return;
        }
        UnicloudTypeEnum[] values = UnicloudTypeEnum.values();
        for (UnicloudTypeEnum unicloudTypeEnum : values) {
            VirtualizationBasicInfoDto basicInfoDto = new VirtualizationBasicInfoDto();
            if(vmTypeMap.containsKey(unicloudTypeEnum.getType())){
                basicInfoDto.setName(unicloudTypeEnum.getDesc());
                basicInfoDto.setValue(vmTypeMap.get(unicloudTypeEnum.getType()).size());
                basicInfoDtos.add(basicInfoDto);
            }
        }
        root.setBasicInfoData(basicInfoDtos);
    }
}
